var today = new Date(),lastweek = new Date(),lastMonth = new Date(),nextMonth= new Date(),nextweek= new Date();
var nextweek  = new Date(nextweek.setDate(today.getDate() + 7));
var lastweek  = new Date(lastweek.setDate(today.getDate() - 7));
var nextMonth = new Date(nextMonth.setMonth(today.getMonth() + 1));
var lastMonth  = new Date(lastMonth.setMonth(today.getMonth() - 1));

$("#start").calendar();
$("#end").calendar();
$('.calendar').click(function () {
    $('.searchBtn').css("background","transparent");
});
$('.searchBtn').click(function () {
    $(this).addClass('weui-bar__item--on').siblings().removeClass('weui-bar__item--on');
    $(this).css({"background":"red"}).siblings().css("background","transparent");
});

function successLoadData(data) {
    var con='';
    if(data.noresult){
        $('.result').html(data.noresult)
    }
    if(data.result){
        var list =data.result;
        list.forEach(function (e,index) {
            for(var i in e){
                con +=  '<div class="weui_cell" style="padding: 5px;display: block;"><span>'+i+e[i]+'</span></div>';
            }
        });
        $('.result').html(con)
    }
}
function loadThisWeekData(condition) {
    $('.result').html('<span class="weui-loading" ></span>');
    st = today.format("yyyy-MM-dd");
    en = nextweek.format("yyyy-MM-dd");
    $('#start').val(st);
    $('#end').val(en);
    Dolphin.ajax({
        url:contextData.contextPath+condition+'?cardCode='+urlParse(url).cardCode+'&startDate='+st+'&endDate='+en,
        type : 'get',
        onSuccess:function (data) {
            successLoadData(data)
        },
        onError:function (e) {
            alert('网络出错啦，请重新加载')
        }
    })
}
function loadLastWeekData(condition) {
    $('.result').html('<span class="weui-loading" ></span>');
    st = lastweek.format("yyyy-MM-dd");
    en = today.format("yyyy-MM-dd");
    $('#start').val(st);
    $('#end').val(en);
    Dolphin.ajax({
        url:contextData.contextPath+condition+'?cardCode='+urlParse(url).cardCode+'&startDate='+st+'&endDate='+en,
        type : 'get',
        onSuccess:function (data) {
            successLoadData(data)
        },
        onError:function (e) {
            alert('网络出错啦，请重新加载')
        }
    })
}
function loadThisMonthData(condition) {
    $('.result').html('<span class="weui-loading" ></span>');
    st = today.format("yyyy-MM-dd");
    en = nextMonth.format("yyyy-MM-dd");
    $('#start').val(st);
    $('#end').val(en);
    Dolphin.ajax({
        url:contextData.contextPath+condition+'?cardCode='+urlParse(url).cardCode+'&startDate='+st+'&endDate='+en,
        type : 'get',
        onSuccess:function (data) {
            successLoadData(data)
        },
        onError:function (e) {
            alert('网络出错啦，请重新加载')
        }
    })
}
function loadLastMonthData(condition) {
    $('.result').html('<span class="weui-loading" ></span>');
    st = lastMonth.format("yyyy-MM-dd");
    en = today.format("yyyy-MM-dd");
    $('#start').val(st);
    $('#end').val(en);
    Dolphin.ajax({
        url:contextData.contextPath+condition+'?cardCode='+urlParse(url).cardCode+'&startDate='+st+'&endDate='+en,
        type : 'get',
        onSuccess:function (data) {
            successLoadData(data)
        },
        onError:function (e) {
            alert('网络出错啦，请重新加载')
        }
    })
}
function search(condition) {
    st = $('#start').val();
    en = $('#end').val();
    if(st&&en){
        Dolphin.ajax({
            url:contextData.contextPath+condition+'?cardCode='+urlParse(url).cardCode+'&startDate='+st+'&endDate='+en,
            type : 'get',
            onSuccess:function (data) {
                successLoadData(data)
            },
            onError:function (e) {
                alert('网络出错啦，请重新加载')
            }
        })
    }else{
        $.toptip('请选择正确时间', 'error');
    }
}
function urlParse(url) {
    var result = {}
    var elems = url.split('?')[1].split('&')

    elems.forEach(function(e){
        var keyVal = e.split('='),
            key = keyVal[0]
        val = keyVal[1] || ''       //没有value的情况下表示为‘’而非undefined

        if(result[key] !== void 0){     //属性有两个以上的值，存储为字符串数组
            console.log(result[key]);
            result[key] = [].concat(result[key]).concat(val)
        }else{                          //只有一个属性值，以字符串形式存储
            result[key] = val
        }
    })

    return result
}
Date.prototype.format = function(format)
{
    var o = {
        "M+" : this.getMonth()+1, //month
        "d+" : this.getDate(), //day
        "h+" : this.getHours(), //hour
        "m+" : this.getMinutes(), //minute
        "s+" : this.getSeconds(), //second
        "q+" : Math.floor((this.getMonth()+3)/3), //quarter
        "S" : this.getMilliseconds() //millisecond
    }
    if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
        (this.getFullYear()+"").substr(4- RegExp.$1.length));
    for(var k in o)if(new RegExp("("+ k +")").test(format))
        format = format.replace(RegExp.$1,
            RegExp.$1.length==1? o[k] :
                ("00"+ o[k]).substr((""+ o[k]).length));
    return format;
}